package com.yanqu.road.dao.impl.activity.newyearbag;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.activity.newyearbag.NewYearBagDrawConfig;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NewYearBagDrawConfigDaoImpl extends TempDao {

    private static String getNewYearBagDrawConfigSql = "select * from `t_s_activity_newyearbag_draw` where `activityId`=?";

    public List<NewYearBagDrawConfig> getNewYearBagDrawConfigList(int activityId) {
        List<NewYearBagDrawConfig> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getNewYearBagDrawConfigSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                NewYearBagDrawConfig newYearBagDrawConfig = new NewYearBagDrawConfig();
                newYearBagDrawConfig.setActivityId(rs.getInt("activityId"));
                newYearBagDrawConfig.setRewardId(rs.getInt("id"));
                newYearBagDrawConfig.setRewardType(rs.getInt("rewardType"));
                newYearBagDrawConfig.setReward(rs.getString("reward"));
                newYearBagDrawConfig.setWeightList(StringUtils.stringToIntegerList(rs.getString("weight"), "\\|"));
                newYearBagDrawConfig.setTitleLimit(rs.getInt("limit"));

                newYearBagDrawConfig.setIsBigAward(rs.getInt("isBigAward"));
                String itemLimit = rs.getString("itemLimit");
                if(!"0".equals(itemLimit) && !StringUtils.isNullOrEmpty(itemLimit)){
                    List<String> daysLimit = StringUtils.stringToStringList(itemLimit, "\\|");
                    for (String str : daysLimit) {
                        List<Integer> paramList = StringUtils.stringToIntegerList(str, "=");
                        if(paramList.size() == 2){
                            newYearBagDrawConfig.getItemLimitMap().put(paramList.get(0), paramList.get(1));
                        }
                    }
                }
                newYearBagDrawConfig.setChangeWeight(rs.getString("shiwuChangeWeight"));

                list.add(newYearBagDrawConfig);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }
}
